package com.yazi.mxz.common.db.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicDao;
import com.yazi.mxz.common.db.dao.MemberNoticeDao;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.MemberNotice;
import com.yazi.mxz.common.entity.beans.Notice;

@Repository("memberNoticeDao")
public class MemberNoticeDaoImpl extends BasicDao<MemberNotice> implements
		MemberNoticeDao {

	public MemberNoticeDaoImpl() {
		super(MemberNotice.class);

	}

	@Override
	public Criteria getExample(MemberNotice mn, String searchWord) {
		Criteria c = super.getExample(mn, searchWord);

		// 关联对象条件
		if (null != mn) {
			// 用户
			Member member = mn.getMember();
			if (null != member) {
				// 用户ID
				if (null != member.getMemberId()) {
					c.add(Restrictions.eq("member.memberId", member.getMemberId()));
				} else {
					// ...用户其它条件
				}
			}
			// 通知
			Notice notice = mn.getNotice();
			if (null != notice) {
				// 通知ID
				if (null != notice.getNoticeId()) {
					c.add(Restrictions.eq("notice.noticeId", notice.getNoticeId()));
				} else {
					// ...通知其它条件
				}
			}
			// ...其它关联对象条件
		}

		// ...关键字条件
		return c;
	}

	@Override
	public int deleteByNoticeId(Long noticeId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("noticeId", noticeId);
		return executeUpdate("delByNoticeIdHql", map);
	}

	@Override
	public int delete(long memberId, long[] noticeIds) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		map.put("noticeIds", noticeIds);
		return executeUpdate("delByMemberIdNoticeIdsHql", map);
	}

	@Override
	public void update(Long memberId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		executeUpdate("updateByMemberIdHql", map);

	}

	@Override
	public Page<MemberNotice> listByMemberId(Long memberId, Searching si) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		return list("listByMemberIdHql", map, si);
	}

}
